% GAUTIER LE BIHAN - 2020
% Replication files for "Shocks vs Menu Costs: Patterns of Price Rigidity in an Estimated Multi-Sector Menu-Cost Model?" Review of Economics and Statistics
%
% This code produces Figure 2

clear 
close all

cd ..\..\Simulations_VC\MS_produits_CPlus

load ..\..\Estimation_param\MS_produits_CPlus\actual_moments_k
weight=actual_moments_k(:,12);
sum(weight)

load  ..\..\Simulations_VC\MS_produits_CPlus\stat_d
param=stat_d;


sect=actual_moments_k(:,1);

p0=param(:,1);
%muc=param(:,2);
sig_ea=param(:,3);
rho_a=param(:,4);
muc=param(:,end-1);  % i.e. /theta YSS
calvo_share=param(:,end);  % i.e. /theta YSS

param0=[p0 muc sig_ea rho_a calvo_share];

mean(param0)
meanw_data_all=w'*param0/sum(w)

param0=param0( muc<0.15 & sect~=5,:)
w=w( muc<0.15 & sect~=5,:)
sect=sect( muc<0.15 & sect~=5,:)

p0=param0(:,1);
muc=param0(:,2);
sig_ea=param0(:,3);
rho_a=param0(:,4);
calvo_share=param0(:,5);

size_param=size(param0,2)

sig=sig_ea./sqrt(1-rho_a.*rho_a);

bins=42;
maxy=13;
%freq
max=0.16;
min=0.000;
%bins=80;
binValues_f=(min:(max-min)/bins:max);
[histw histv] = histwv(p0((sect==1  & p0<max)), w(sect==1  & p0<max,:), min, max, bins+1); 
counts1 = histw ;
[histw histv] = histwv(p0((sect==2  & p0<max)), w(sect==2  & p0<max,:), min, max, bins+1); 
counts2 =  histw ;
[histw histv] = histwv(p0((sect==4  & p0<max)), w(sect==4  & p0<max,:), min, max, bins+1); 
counts4 = histw;
%[histw histv] = histwv(p0((sect(:,1)==5 & p0<max)), w(sect==5,:), min, max, bins+1) 
%counts5 = histw;

[histw histv] = histwv(p0(sect==6 & p0<max), w(sect==6 & p0<max,:), min, max, bins+1); 
counts6 =histw;
counts_all = counts1 + counts2 + counts4+counts6;   
sum1=cumsum(counts_all);
sum=sum1(end);  
%counts_a = counts1 + counts2 + counts4+counts5;   
counts_b = counts1 + counts2 + counts4;   
counts_c = counts1  ;   
%counts_af=counts_a./sum;
counts_bf=counts_b./sum*100;
counts_cf=counts_c./sum*100;
countsf=counts_all./sum*100;

figure;
subplot(2,2,1);
bar(binValues_f, countsf, 'FaceColor',[0.2 0.2 0.2], 'barwidth', 1);
%hold on
%bar(binValues_f, counts_af, 'k', 'barwidth', 1)
hold on
bar(binValues_f, counts_bf, 'FaceColor',[0.6 0.6 0.6], 'barwidth', 1);
hold on
bar(binValues_f, counts_cf, 'w', 'barwidth', 1);
hold off
hold on
plot([0.0465 0.0465],[0 maxy],'-k','Linewidth',1);
hold on
plot([0.1167 .1167],[0 maxy],'--k','Linewidth',1);


%lgd=legend('Services', 'Manuf', 'Food', 'Median', 'Mean')
%lgd.FontSize = 5;
% legend('Location','best')
axis([min max 0 maxy])
  xlabel('\lambda');
 ylabel('[%]'); 

 
 
 

%mu
max=0.15;
min=0.0;
%bins=80;
binValues_f=(min:(max-min)/bins:max);
[histw histv] = histwv(muc((sect==1)), w(sect==1,:), min, max, bins+1) ;
counts1 = histw ;
[histw histv] = histwv(muc((sect==2)), w(sect==2,:), min, max, bins+1); 
counts2 =  histw ;
[histw histv] = histwv(muc((sect==4)), w(sect==4,:), min, max, bins+1) ;
counts4 = histw;
%[histw histv] = histwv(muc((sect==5)), w(sect==5,:), min, max, bins+1) ;
%counts5 = histw;

[histw histv] = histwv(muc(sect==6), w(sect==6,:), min, max, bins+1) ;
counts6 =histw;
counts_all = counts1 + counts2 + counts4+counts6;   
sum1=cumsum(counts_all);
sum=sum1(end);  
counts_b = counts1 + counts2 + counts4;   
counts_c = counts1  ;   
%counts_af=counts_a./sum;
counts_bf=counts_b./sum*100;
counts_cf=counts_c./sum*100;
countsf=counts_all./sum*100;

subplot(2,2,2);
bar(binValues_f, countsf, 'FaceColor',[0.2 0.2 0.2], 'barwidth', 1);
%hold on
%bar(binValues_f, counts_af, 'k', 'barwidth', 1)
hold on
bar(binValues_f, counts_bf, 'FaceColor',[0.6 0.6 0.6], 'barwidth', 1);
hold on
bar(binValues_f, counts_cf, 'w', 'barwidth', 1);
hold off
hold on
plot([0.0411 0.0411],[0 maxy],'-k','Linewidth',1);
hold on
plot([0.0612 0.0612],[0 maxy],'--k','Linewidth',1);


lgd=legend('Services','Manuf', 'Food', 'Median', 'Mean')
 legend('Location','best')
 lgd.FontSize = 5;
axis([min max 0 maxy])
  xlabel('\mu');
 ylabel('[%]'); 

 
 
%sigma
max=0.095;
min=0.0;
%bins=80;
binValues_f=(min:(max-min)/bins:max);
[histw histv] = histwv(sig_ea((sect==1)), w(sect==1,:), min, max, bins+1) ;
counts1 = histw ;
[histw histv] = histwv(sig_ea((sect==2)), w(sect==2,:), min, max, bins+1) ;
counts2 =  histw ;
[histw histv] = histwv(sig_ea((sect==4)), w(sect==4,:), min, max, bins+1); 
counts4 = histw;
%[histw histv] = histwv(sig_ea((sect==5)), w(sect==5,:), min, max, bins+1); 
%counts5 = histw;

[histw histv] = histwv(sig_ea(sect==6), w(sect==6,:), min, max, bins+1) ;
counts6 =histw;
counts_all = counts1 + counts2 + counts4+counts6;   
sum1=cumsum(counts_all);
sum=sum1(end);  
counts_b = counts1 + counts2 + counts4;   
counts_c = counts1  ;   
%counts_af=counts_a./sum;
counts_bf=counts_b./sum*100;
counts_cf=counts_c./sum*100;
countsf=counts_all./sum*100;

%subplot(2,2,2);


%figure;
subplot(2,2,3);
bar(binValues_f, countsf, 'FaceColor',[0.2 0.2 0.2], 'barwidth', 1);
%hold on
%bar(binValues_f, counts_af, 'k', 'barwidth', 1)
hold on
bar(binValues_f, counts_bf, 'FaceColor',[0.6 0.6 0.6], 'barwidth', 1);
hold on
bar(binValues_f, counts_cf, 'w', 'barwidth', 1);
hold off
hold on
plot([0.0441 0.0441],[0 maxy],'-k','Linewidth',1);
hold on
 plot([.0429 0.0429],[0 maxy],'--k','Linewidth',1);

axis([min max 0 maxy])
  xlabel('\sigma');
 ylabel('[%]'); 

max=1;
min=0.1;
bins=40;
binValues_f=(min:(max-min)/bins:max);
[histw histv] = histwv(calvo_share((sect==1)& calvo_share>min), w(sect==1 & calvo_share>min,:), min, max, bins+1) ;
counts1 = histw ;
[histw histv] = histwv(calvo_share((sect==2)& calvo_share>min), w(sect==2 & calvo_share>min,:), min, max, bins+1) ;
counts2 =  histw ;
[histw histv] = histwv(calvo_share((sect==4)& calvo_share>min), w(sect==4 & calvo_share>min,:), min, max, bins+1) ;
counts4 = histw;
[histw histv] = histwv(calvo_share((sect==5)& calvo_share>min), w(sect==5 & calvo_share>min,:), min, max, bins+1) ;
counts5 = histw;

[histw histv] = histwv(calvo_share(sect==6& calvo_share>min), w(sect==6 & calvo_share>min,:), min, max, bins+1) ;
counts6 =histw;
counts_all = counts1 + counts2 + counts4+counts6;   
sum1=cumsum(counts_all);
sum=sum1(end);  
counts_a = counts1 + counts2 + counts4+counts5;   
counts_b = counts1 + counts2 + counts4;   
counts_c = counts1  ;   
counts_af=counts_a./sum*100;
counts_bf=counts_b./sum*100;
counts_cf=counts_c./sum*100;
countsf=counts_all./sum*100;


print('..\..\..\figures\figure3.pdf','-dpdf')
print('..\..\..\figures\figure3','-depsc')
